package com.qf.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.mapper.DtsAdminMapper;
import com.qf.mapper.DtsBrandMapper;
import com.qf.pojo.DtsAdmin;
import com.qf.pojo.DtsBrand;
import com.qf.service.DtsAdminService;
import com.qf.util.bcrypt.BCryptPasswordEncoder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author bss
 * @version V1.0
 * @Project maven
 * @Package com.qf.service.impl
 * @Description:
 * @Date 2022/10/13 17:27
 */
@Service
public class DtsAdminServiceImpl implements DtsAdminService {

    @Autowired
    DtsAdminMapper adminMapper;

    @Override
    public DtsAdmin findByUsername(String username) {
        LambdaQueryWrapper<DtsAdmin> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DtsAdmin::getUsername,username);
        DtsAdmin admin = adminMapper.selectOne(wrapper);
        return admin;
    }

    @Override
    public Map<String, Object> findByPage(Integer page, Integer limit, String sort, String order, String username) {
        IPage<DtsAdmin> iPage = new Page<>(page,limit);
        QueryWrapper<DtsAdmin> wrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(username)){
            wrapper.like("username", username);
        }
        if ("desc".equalsIgnoreCase(order)){
            wrapper.orderByDesc(sort);
        }else {
            wrapper.orderByAsc(sort);
        }

        adminMapper.selectPage(iPage, wrapper);
        List<DtsAdmin> adminList = iPage.getRecords();
        adminList = adminList.stream().map(admin -> {
            admin.setPassword("");
            return admin;
        }).collect(Collectors.toList());

        Map<String, Object> map = new HashMap<>();
        map.put("total", iPage.getTotal());
        map.put("items", adminList);

        return map;
    }

    @Override
    public void insert(DtsAdmin admin) {

        admin.setAddTime(new Date());
        admin.setUpdateTime(new Date());
        admin.setDeleted(false);
        BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
        String encode = passwordEncoder.encode(admin.getPassword());
        admin.setPassword(encode);

        adminMapper.insert(admin);
    }

    @Override
    public void update(DtsAdmin admin) {
        admin.setUpdateTime(new Date());
        BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
        String encode = passwordEncoder.encode(admin.getPassword());
        admin.setPassword(encode);
        adminMapper.updateById(admin);
    }

    @Autowired
    DtsBrandMapper brandMapper;

    @Override
    public void delete(DtsAdmin admin) {

        LambdaQueryWrapper<DtsBrand> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DtsBrand::getAdminId,admin.getId());
        wrapper.eq(DtsBrand::getDeleted,false);
//        查询是否被其他表关联
        Integer count = brandMapper.selectCount(wrapper);
//        如果count > 0  说明管理员关联品牌数据 ，提醒前端不能逻辑删除
        if (count > 0){
            throw new  RuntimeException("该管理员被品牌关联，不能删除");
        }
        admin.setDeleted(true);
        adminMapper.updateById(admin);
    }

    @Override
    public List<Map<String, Object>> findAll() {
        LambdaQueryWrapper<DtsAdmin> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DtsAdmin::getDeleted,false);
        List<DtsAdmin> adminList = adminMapper.selectList(wrapper);

        List<Map<String, Object>> list = adminList.stream().map(admin -> {
            Map<String, Object> map = new HashMap<>();
            map.put("value", admin.getId());
            map.put("label", admin.getUsername());
            return map;
        }).collect(Collectors.toList());
        return list;
    }
}
